package com.qujia.station.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

@Slf4j
public class yunHuaPayUtils {


    public static HashMap yunHuaPay(YunHuaParam yunHuaParam){
        String  res="";
        try {
            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/unionPay", yunHuaParam.toRequest());
             JSONObject result= JSONObject.parseObject(res);
            if(!"0000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
            }
            JSONObject data= JSONObject.parseObject(result.get("data")+"");
            if(!"T".equals(data.getString("status"))){
                log.info("云华支付，回调data的状态不正确，错误信息"+data.get("errMsg"));
            }
            JSONObject payParam= JSONObject.parseObject(data.getString("payInfo"));
            HashMap reqData = new HashMap();

                reqData.put("payTimeStamp", payParam.get("timeStamp"));
                reqData.put("payPackage", payParam.get("package"));
                reqData.put("paynonceStr", payParam.get("nonceStr"));
                reqData.put("paySignType", payParam.get("signType"));
                reqData.put("paySign", payParam.get("paySign"));
                reqData.put("merchantsNumber", data.get("txnOrderNo"));

       return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();

    }

    public static HashMap yunHuaRefund(YunHuaRefundParam yunHuaRefundParam){
        String  res="";
        try {
            String  beforeSign=yunHuaRefundParam.toBeforeSign();
             String  sign= Sha256SignCreator.createSign(beforeSign,"1234567890");
            yunHuaRefundParam.setSign(sign);
            res=  HttpUtils.connectPostUrl("https://yh.xailife.com/marketing-dev01/pay/refund", yunHuaRefundParam.toRefundAmt());
            JSONObject result= JSONObject.parseObject(res);
            if(!"0000".equals(result.get("code")+"")){
                log.info("云华拉起支付失败，返回code"+result.get("code"));
            }
            JSONObject data= JSONObject.parseObject(result.get("data")+"");
            if(!"T".equals(data.getString("tbRefundStatus"))){
                log.info("云华支付，回调data的状态不正确，错误信息"+data.get("errMsg"));
            }
            HashMap reqData = new HashMap();
            return reqData;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new HashMap();

    }




}
